package cn.fzkj.gulimall.coupon.service.impl;

import cn.fzkj.common.to.MemberPrice;
import cn.fzkj.common.to.SkuReductionTo;
import cn.fzkj.gulimall.coupon.entity.MemberPriceEntity;
import cn.fzkj.gulimall.coupon.entity.SkuLadderEntity;
import cn.fzkj.gulimall.coupon.service.MemberPriceService;
import cn.fzkj.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.fzkj.common.utils.PageUtils;
import cn.fzkj.common.utils.Query;

import cn.fzkj.gulimall.coupon.dao.SkuFullReductionDao;
import cn.fzkj.gulimall.coupon.entity.SkuFullReductionEntity;
import cn.fzkj.gulimall.coupon.service.SkuFullReductionService;
import org.springframework.util.CollectionUtils;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService ladderService;
    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public boolean saveSkuReduction(SkuReductionTo to) {

        // 1、保存满减打折、会员价
        if (to.getFullCount() > 0) {
            SkuLadderEntity ladderEntity = new SkuLadderEntity();
            ladderEntity.setFullCount(to.getFullCount());
            ladderEntity.setDiscount(to.getDiscount());
            ladderEntity.setSkuId(to.getSkuId());
            ladderEntity.setAddOther(to.getCountStatus());

            ladderService.save(ladderEntity);
        }

        // 2、保存满减信息
        if (to.getFullPrice().compareTo(new BigDecimal("0")) == 1) {
            SkuFullReductionEntity fullReductionEntity = new SkuFullReductionEntity();
            BeanUtils.copyProperties(to, fullReductionEntity);
            this.save(fullReductionEntity);
        }

        // 3、会员价格
        if (!CollectionUtils.isEmpty(to.getMemberPrice())) {
            List<MemberPriceEntity> collect = to.getMemberPrice().stream().map(price -> {
                MemberPriceEntity entity = new MemberPriceEntity();
                entity.setSkuId(to.getSkuId());
                entity.setMemberLevelId(price.getId());
                entity.setMemberLevelName(price.getName());
                entity.setMemberPrice(price.getPrice());
                entity.setAddOther(1);
                return entity;

            }).filter(item -> {
                return item.getMemberPrice().compareTo(new BigDecimal("0")) == 1;
            }).collect(Collectors.toList());
            memberPriceService.saveBatch(collect);
        }

        return true;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

}